from pydantic_settings import BaseSettings
from typing import Optional


class Settings(BaseSettings):
    """
    应用配置类
    使用pydantic-settings管理配置，支持从环境变量读取
    """

    # 数据库配置
    postgres_user: str = "jiulang"
    postgres_password: str = "999999999"
    postgres_db: str = "postgres"
    postgres_host: str = "175.178.98.6"
    postgres_port: int = 9932

    # 数据库心跳配置
    db_heartbeat_enabled: bool = True
    db_heartbeat_interval: int = 300  # 5分钟

    # 数据库重连配置
    db_reconnect_enabled: bool = True
    db_reconnect_max_retries: int = 3
    db_reconnect_retry_interval: int = 5  # 重试间隔秒数

    # 应用配置
    app_name: str = "词仙笔记后端服务"
    debug: bool = True

    # 日志配置
    log_level: str = "INFO"

    @property
    def database_url(self) -> str:
        """
        构建PostgreSQL数据库连接URL
        """
        return (
            f"postgresql+asyncpg://{self.postgres_user}:{self.postgres_password}"
            f"@{self.postgres_host}:{self.postgres_port}/{self.postgres_db}"
        )

    class Config:
        env_file = (".env", ".env.example")
        env_file_encoding = "utf-8"
        case_sensitive = False


# 全局配置实例
settings = Settings()
